/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 24, 2012  4:15:55 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : SyncPrServices.java
 * TODO
 */
package jpa.service.promis.construction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.promis.construction.PurchaseRequestPK;
import jpa.entitys.promis.construction.SyncPr;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class SyncPrServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(SyncPrServices.class);

	/**
	 * @param em
	 */
	public SyncPrServices(EntityManager em) {
		super(em);
	}

	/**
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public SyncPr findById(String id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "SyncPr.findById"));				
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<SyncPr> findAll() throws Exception {
		return super.ConvertToYoureObject(super.findAll("SyncPr.findAll"));
	}
	
	/**
	 * 
	 * @param purchaseRequestPK
	 * @return
	 * @throws Exception
	 */
	public List<SyncPr> findByPrimary(PurchaseRequestPK purchaseRequestPK) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", purchaseRequestPK.getCompany());
		mapWhereValue.put("project", purchaseRequestPK.getProject());
		mapWhereValue.put("porType", purchaseRequestPK.getPorType());
		mapWhereValue.put("porNo", purchaseRequestPK.getPorNo());
		mapWhereValue.put("porSeries", purchaseRequestPK.getPorSeries());
		
		return super.ConvertToYoureObject(super.find("SyncPr.findByPrimary", mapWhereValue));
		
	}
	
	/**
	 * 
	 * @param status
	 * @param syncIsstaging
	 * @return
	 * @throws Exception
	 */
	public List<SyncPr> findByPRStatus(String status, String syncIsstaging) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("status", status);
		mapWhereValue.put("syncIsstaging", syncIsstaging);
		
		return super.ConvertToYoureObject(super.find("SyncPr.findByPRStatus", mapWhereValue));
	}
	
	/**
	 * 
	 * @param project
	 * @param documentNo
	 * @param status
	 * @return
	 * @throws Exception
	 */
	public SyncPr findByDocumentNo(String project ,String documentNo, String status) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();				
		mapWhereValue.put("project", project);
		mapWhereValue.put("documentNo", documentNo);
		mapWhereValue.put("status", status);
		
		List<SyncPr> lst = super.ConvertToYoureObject(super.find("SyncPr.findByDocumentNo", mapWhereValue));
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else if(CommonService.getSize(lst) > 1)
			throw new Exception("Error has more then one [rec size=" + CommonService.getSize(lst) + "]");
		else
			return null;
	}
	
	/**
	 * 
	 * Date =Nov 14, 2013 : 10:05:44 AM 
	 * CreateBy=Chatree Siangklom
	 * SyncPr
	 * TODO
	 * @param company
	 * @param project
	 * @param documentNo
	 * @return
	 * @throws Exception
	 */
	public SyncPr findByDocumentNo2(String company, String project ,String documentNo) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();				
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("documentNo", documentNo);
		
		List<SyncPr> lst = super.ConvertToYoureObject(super.find("SyncPr.findByDocumentNo2", mapWhereValue));
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else if(CommonService.getSize(lst) > 1)
			throw new Exception("Error has more then one [rec size=" + CommonService.getSize(lst) + "]");
		else
			return null;
	}
	
	/**
	 * 
	 * @param company
	 * @param project
	 * @param documentNo
	 * @param status
	 * @param syncIsstaging
	 * @return
	 * @throws Exception
	 */
	public SyncPr findByDocumentNo1(String company, String project, String documentNo, String status, String syncIsstaging) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);									System.out.println("company : " + company);
		mapWhereValue.put("project", project);									System.out.println("project : " + project);
		mapWhereValue.put("documentNo", documentNo);							System.out.println("documentNo : " + documentNo);
		mapWhereValue.put("status", status);									System.out.println("status : " + status);
		mapWhereValue.put("syncIsstaging", syncIsstaging);						System.out.println("syncIsstaging : " + syncIsstaging);
		
		List<SyncPr> lst = super.ConvertToYoureObject(super.find("SyncPr.findByDocumentNo1", mapWhereValue));
		
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else if(CommonService.getSize(lst) > 1)
			throw new Exception("Error has more then one [rec size=" + CommonService.getSize(lst) + "]");
		else
			return null;
	}
	
}
